<?php

namespace system\Controller;

class MenusController extends SystemAppController {

    public $helpers = ['Pdf'];

    public function indexAction(){
        $modulos = $this->_model->getModulos();
        $this->set("modulos",$modulos);
        $where = ($this->request->is("post")) ? $this->_model->filter($this->request->post()) : null;
        $this->index($where);
    }

    public function addAction()
    {
        $this->set("modulos", $this->_model->getModulos());
        $this->set("controllers", $this->_model->getControllers());
        $this->set("data",'');
        $this->add();
    }

    public function editAction($id)
    {
        if(!empty($id)){
            $dados = $this->_model->loadbyid($id);
            $dados ? $this->set("data",$dados) : false ;

            $modulos = $this->_model->getModulos();
            $controllers = $this->_model->getControllers();
            $menus = $this->_model->getMenus($dados->modulo_id);
            $this->set(compact('modulos','controllers','menus'));
            $this->render('edit');
        }else{
            $this->Session->setFlash($this->msgType('registro'),'warning');
            $this->redirect(array('action'=>'index'));
        }
    }

    public function viewAction($id){
        if(!empty($id)){
            $dados = $this->_model->loadbyid($id);
            $dados->modulo = !empty($dados->modulo_id)?$this->_model->returnField("modulos","DESCRICAO", $dados->modulo_id):'';
            $dados->controller = !empty($dados->controllers_id)?$this->_model->returnField("controllers","DESCRICAO", $dados->controllers_id):'';
            $dados->menuPai = !empty($dados->menu_pai_id)?$this->_model->returnField("menus","TITULO", $dados->menu_pai_id):'';
            $dados ? $this->set("data",$dados) : false ;
        }
        $this->render('view');
    }

    public function delAction(){
        $this->del();
    }

    public function pdfAjax($id=null){
//        $this->autoRender(false);
        $dados = $this->_model->listing($this->request->params());
        $modulos = $this->_model->getModulos();
        $this->set("count",$this->_model->countAll()->executeCount());
        if($dados){
            $this->set("modulos",$modulos);
            $this->set("listData",$dados);
        }
        $html = $this->render('print');
        $this->HpPdf->title('Lista Menu');
        $this->HpPdf->setStylePage();
//        $this->HpPdf->nameRand('.pdf');
        $this->HpPdf->getFileCss('reports.css');
        $this->HpPdf->writeBody($html);
        $this->HpPdf->save();
//        $this->exportCSV();
    }

    public function statusAction(){
        $this->status();
    }

    /** mostra o select dos menus pai que existem com base no modulo */
    public function getMenuPaiAjax(){
        $this->autoRender(false);
        $result = ['HTML'=>null];
        if($this->request->is('post')){
            $dados = null;
            if(isset($_POST["menu_id"])) {
                $dados = $this->_model->loadbyid($this->request->post("menu_id"));
            }
            $this->set('modulo_id',$this->request->post('id'));
            $this->set("data", $dados);
            $this->set("menus", $this->_model->getMenus($this->request->post("id")));
            $result['HTML'] = $this->render("_menu_pai");
        }
        return $this->json($result);
    }

    public function getMenuPaiFilterAjax(){
        if($this->request->is('post')){
            $dados = null;
            $this->set("menus", $this->_model->getMenusPai($this->request->post("id")));
            $this->charset();
            $this->render("_menu_pai_filter");
        }
    }

    /** carrega a tpl do form do menu pai modal */
    public function novoMenuPaiAjax(){
        $this->autoRender(false);
        $this->set('modulo_id',$this->request->post('modulo_id'));
        $this->set("modulos", $this->_model->getModulos());
        $this->set("controllers", $this->_model->getControllers());
        $html = $this->render('_form_menu_pai');
        return $this->json(['HTML'=>$html]);
    }

    /** salvar do menu pai vindo do modal */
    public function saveMenuPaiAjax(){
        $this->autoRender(false);
        $result = ['HTML'=>null,'STATUS'=>false];
        if($this->request->is('post')){
           if($this->_model->saveMenuPai($this->request->post())){
               $result['STATUS'] = true;
           }
            $this->set('modulo_id',$this->request->post('modulo_id'));
            $this->set("menus", $this->_model->getMenus($this->request->post("modulo_id")));
            $html = $this->render("_menu_pai");
            $result['HTML'] = $html;
        }
        return $this->json($result);
    }

}